package logic

import (
	"context"
	"encoding/json"
	"novel-world/model/cache"
	"novel-world/model/mysql"
	"time"

	"novel-world/novelrpc/internal/svc"
	"novel-world/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *novelrpc.GoodsListRequest) (*novelrpc.GoodsListResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.Goods
	var lists []*novelrpc.GetList2
	result, _ := cache.Rdb.Get(cache.Ctx, "goodsList").Result()
	if result == "" {
		var err error
		lists, err = user.GoodsList(mysql.DB, lists, in)
		if err != nil {
			return nil, err
		}
		jsonData, _ := json.Marshal(lists)
		cache.Rdb.Set(cache.Ctx, "goodsList", jsonData, time.Minute*15)
	} else {
		json.Unmarshal([]byte(result), &lists)
	}

	return &novelrpc.GoodsListResponse{
		List1: lists,
	}, nil
}
